#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

void read_childproc(int signo) {
  int status;
  pid_t pid;

  pid = waitpid(-1, &status, WNOHANG);
  if (WIFEXITED(status)) {
    printf("Remove process id: %d \n", pid);
    printf("Child send: %d \n", WEXITSTATUS(status));
  } else {
    printf("Remove child process failed\n");
  }
}

// int main(int argc, char *argv[]) {
int main(void) {
  pid_t pid;
  struct sigaction act;

  act.sa_handler = read_childproc;
  sigemptyset(&act.sa_mask);
  act.sa_flags = 0;
  sigaction(SIGCHLD, &act, 0);

  pid = fork();
  if (pid > 0) {
    printf("I'am Parent, my pid:%d\n", getpid());
    sleep(10);
  } else if (pid == 0) {
    printf("I'am child, my pid:%d\n", getpid());
    return 12;
  } else {
    printf("fork() error!\n");
  }

  return 0;
}

/*

make
sudo make rootfs

kernel/fork.c
  b start_kernel
  b dup_task_struct

target remote : 1234

*/
